package com.mune.gulimall.product.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mune.gulimall.product.entity.CategoryEntity;
import com.mune.gulimall.product.service.CategoryService;
import com.mune.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
  @Autowired
  CategoryService categoryService;

  @GetMapping(value = {"/", "index.html"})
  public String indexPage(Model model) {
    List<CategoryEntity> list = categoryService.getLevel1Categorys();
    model.addAttribute("category", list);
    return "index";
  }

  @ResponseBody
  @GetMapping("/index/json/catalog.json")
  public Map<String, List<Catelog2Vo>> getCatalogJson() throws JsonProcessingException {
    Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
    return map;
  }
}
